/**
* 
* Copyright [2007] [Sunny Liu]
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package ${appConfig.outputPackageName}.web.view;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.view.AbstractView;

import ${appConfig.outputPackageName}.xml.ObjectXMLSerializer;
import ${appConfig.outputPackageName}.xml.ObjectXMLSerializerFactory;
import ${appConfig.outputPackageName}.controller.ServerProcessStatus;


public class FlexXMLView extends AbstractView 
{	
	public static final String NAME = FlexXMLView.class.getName();
	private ObjectXMLSerializerFactory serilizerFactory;
	private static Logger log = LoggerFactory.getLogger(FlexXMLView.class);
	
	/**
	 * 
	 */
	public FlexXMLView() 
	{
		super();
	}
	

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void renderMergedOutputModel(Map map, 
			HttpServletRequest request,
			HttpServletResponse response) throws Exception 
	{
		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma","no-cache");
		response.setDateHeader("Expires", 0); 		
		ServerProcessStatus status = (ServerProcessStatus) map.get(ServerProcessStatus.SERVER_PROCESS_STATUS);
		if(status==null)
		{
			log.warn("No Server process status found. [" + ServerProcessStatus.SERVER_PROCESS_STATUS + "]");
			response.getWriter().print(ObjectXMLSerializer.DEFAULT_ERROR_MESSAGE);
			return;
		}		
		if(status.isHasError()){
			ObjectXMLSerializer serializer = serilizerFactory.matchObjectXMLSerializer(status.getError());
			serializer.serialize(status.getAction(), 599, status.getStackTraceAsString(), response.getOutputStream());
		}else{
			ObjectXMLSerializer serializer = serilizerFactory.matchObjectXMLSerializer(status.getOutput());
			serializer.serialize(status.getAction(), 200, null, status.getOutput(), response.getOutputStream());
		}		
	}
	

	/**
	 * @return Returns the serilizerFactory.
	 */
	public ObjectXMLSerializerFactory getSerilizerFactory() {
		return serilizerFactory;
	}
	/**
	 * @param serilizerFactory The serilizerFactory to set.
	 */
	public void setSerilizerFactory(ObjectXMLSerializerFactory serilizerFactory) {
		this.serilizerFactory = serilizerFactory;
	}
}
